#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>

using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
struct node {
    int h, t;
};
vector<node> g[maxn];

bool cmp(node x, node y) {
    return x.t < y.t;
}

int T[maxn], MX[maxn], d[maxn], vis[maxn];

int main() {
    int n, m, k;
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) {
        int h, t, c;
        cin >> h >> t >> c;
        g[c].push_back(node{h, t});
        T[i] = t;
    }
    int mn = 1e9, ans = -1, cnt = 0;
    sort(T + 1, T + 1 + n);
    int l = unique(T + 1, T + 1 + n) - (T + 1);
    for (int i = 1; i <= k; i++)
        sort(g[i].begin(), g[i].end(), cmp);
    for (int i = 1; i <= l && T[i] <= m; i++) {
        int mn = 1e9;
        for (int j = 1; j <= k; j++) {
            for (; d[j] < g[j].size() && g[j][d[j]].t <= T[i]; d[j]++) {
                node tmp = g[j][d[j]];
                MX[j] = max(tmp.h, MX[j]);
            }
            mn = min(mn, MX[j]);
            if (MX[j] > 0 && vis[j] == 0) {
                vis[j] = 1;
                cnt++;
            }
        }
        if (cnt == k)
            ans = max(ans, m - T[i] + mn);
    }
    cout << ans;
    return 0;
}